package com.example.denglu.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.denglu.dto.UserExcelDto;
import com.example.denglu.entity.User;
import com.example.denglu.mapper.UserMapper;
import com.example.denglu.service.UserService;
import com.example.denglu.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImp extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    UserMapper userMapper;

    public String login(UserVo uservo){//TODO：登陆逻辑函数
        try {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(User::getUsername,uservo.getUsername());
            User userExistN = userMapper.selectOne(wrapper);
            if (userExistN != null){
                String userExistP = userMapper.selectPasswordByUsername(uservo.getUsername());
                if (userExistP.equals(uservo.getPassword())){
                    return uservo.getUsername()+" 用户登录成功,欢迎您！";
                }else {
                    return "登陆失败,密码错误！";
                }
            }else {
                return "登陆失败，账户不存在";
            }
        }catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public String regist(UserVo userVo){//TODO：注册判断逻辑函数
        try{
            User userExist = userMapper.selectByUsername(userVo.getUsername());
             if (userExist != null){
                return "账户已经存在";
            }else {
                userMapper.insertSelective(userVo);
                return "注册成功";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }
    public List<UserExcelDto> Downloads(){

        return userMapper.findAll();
    }
}
